package lms.dwr.demo.service.impl;

import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import lms.dwr.demo.common.DAOException;
import lms.dwr.demo.common.ServiceException;
import lms.dwr.demo.dao.DAOStudent;
import lms.dwr.demo.model.ModelStudent;
import lms.dwr.demo.service.ServiceStudent;
import lms.dwr.demo.utils.StringUtils;
import lms.dwr.demo.web.component.pagination.PaginationSupport;

public class ServiceStudentImpl
implements ServiceStudent
{
	
	/**
	 * The LOGGER
	 */
	private static final Logger LOGGER = Logger.getLogger(ServiceStudentImpl.class);
	
	/**
	 * The student repository
	 */
	private DAOStudent daoStudent;
	
	/*
	 * (non-Javadoc)
	 * @see lms.dwr.demo.service.ServiceStudent#get(java.lang.String)
	 */
	@Override
	public ModelStudent getStudentById(String id) throws ServiceException 
	{
		try 
		{
			return daoStudent.get(id);
		} 
		catch (DAOException e) 
		{
			LOGGER.error("It failed to fetch the student entity by id: " + id, e);
			return null;
		} 
		catch (Exception e)
		{
			throw (new ServiceException("It failed to fetch the student entity by id: " + id, e));
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see lms.dwr.demo.service.ServiceStudent#get(lms.dwr.demo.model.ModelStudent, int, int, java.lang.String, java.lang.Boolean)
	 */
	@Override
	public PaginationSupport<ModelStudent> getStudentPagination(ModelStudent entity,
			int pageSize, int startIndex, String order, Boolean isDesc) throws ServiceException 
	{
		try {
			DetachedCriteria criteria = DetachedCriteria.forClass(ModelStudent.class);
				if (StringUtils.isNotEmpty(entity.getStudentCName())) {
					criteria.add(Restrictions.eq("studentCName", entity.getStudentCName()));
				}
				if (StringUtils.isNotEmpty(entity.getStudentEName())) {
					criteria.add(Restrictions.eq("studentEName", entity.getStudentEName()));
				}
				if (StringUtils.isNotEmpty(entity.getStudentNo())) {
					criteria.add(Restrictions.eq("studentNo", entity.getStudentNo()));
				}
				if (entity.getStudentStatus() != null) {
					criteria.add(Restrictions.eq("studentStatus", entity.getStudentStatus()));
				}
			return daoStudent.findPageByCriteria(criteria, pageSize, startIndex);
		}
		catch(DAOException e)
		{
			LOGGER.error("It failed to fetch pagination of students!", e);
			return null;
		}
		catch (Exception e)
		{
			throw (new ServiceException("It failed to fetch pagination of students!", e));
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see lms.dwr.demo.service.ServiceStudent#getByNo(java.lang.String)
	 */
	@Override
	public ModelStudent getStudentByNo(String studentNo) throws ServiceException 
	{
		ModelStudent entity = new ModelStudent();
		entity.setStudentNo(studentNo);
		
		PaginationSupport<ModelStudent> result = this.getStudentPagination(entity, -1, 0, null, true);
		return result != null ? result.getItems().get(0) : null;
	}

	/*
	 * (non-Javadoc)
	 * @see lms.dwr.demo.service.ServiceStudent#saveOrUpdate(lms.dwr.demo.model.ModelStudent)
	 */
	@Override
	public void saveOrUpdate(ModelStudent entity) throws ServiceException 
	{
		try 
		{
			daoStudent.saveOrUpdate(entity);
		} 
		catch (DAOException e) 
		{
			LOGGER.error("It failed to save/update the student entity!", e);
		}
		catch (Exception e)
		{
			throw (new ServiceException("It failed to save/update the student entity!", e));
		}
	}

	/*
	 * (non-Javadoc)
	 * @see lms.dwr.demo.service.ServiceStudent#remove(java.lang.String)
	 */
	@Override
	public void removeStudentById(String id) throws ServiceException 
	{
		removeStudentByEntity(this.getStudentById(id));
	}
	
	/*
	 * (non-Javadoc)
	 * @see lms.dwr.demo.service.ServiceStudent#remove(lms.dwr.demo.model.ModelStudent)
	 */
	@Override
	public void removeStudentByEntity(ModelStudent entity) throws ServiceException
	{
		try 
		{
			daoStudent.remove(entity);
		} 
		catch (DAOException e) 
		{
			LOGGER.error("It failed to remove the entity!", e);
		}
		catch (Exception e)
		{
			throw (new ServiceException("It failed to remove the entity!", e));
		}
	}
	
	public void setDaoStudent(DAOStudent daoStudent)
	{
		this.daoStudent = daoStudent;
	}

	public DAOStudent getDaoStudent()
	{
		return daoStudent;
	}

	public static Logger getLogger()
	{
		return LOGGER;
	}
}
